
//
//  LXStatusCell.swift
//  weibo
//
//  Created by lixin on 16/4/20.
//  Copyright © 2016年 lixin. All rights reserved.
//

import UIKit

import SnapKit

class LXStatusCell: UITableViewCell {
    
    //记录底部toolBar的约束
    var toolBarTopConstraint: Constraint?

    var status: LXStatus?{
    
        didSet{
            
            originalView.status = status
            
            self.toolBarTopConstraint?.uninstall()//卸载约束
            
            if status?.retweeted_status == nil {
                
                retweetView.hidden = true
                
                toolBarView.snp_makeConstraints(closure: { (make) in
                    
                    self.toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            
            }else{
            
                retweetView.hidden = false
                
                retweetView.status = status
                
                toolBarView.snp_makeConstraints(closure: { (make) in
                    
                    self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }
            
            toolBarView.status = status
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setUI(){
        
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        contentView.addSubview(originalView)
        
        contentView.addSubview(retweetView)
        
        contentView.addSubview(toolBarView)
        
        originalView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentView).offset(8)
            
            make.leading.equalTo(contentView)
            
            make.trailing.equalTo(contentView)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(originalView.snp_bottom)
            
            make.leading.trailing.equalTo(contentView)
           
        }
        
        toolBarView.snp_makeConstraints { (make) -> Void in
            
            self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            
            make.leading.trailing.equalTo(contentView)
            
            make.height.equalTo(37)
        }
     
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.top.equalTo(self)
            
            make.bottom.equalTo(toolBarView)
        }
  
    }

    
    //MARK: - 懒加载控件
    
    //原创微博
    private lazy var originalView: LXStatusOriginalView = LXStatusOriginalView()
    
    //底部转发、评论、赞
    private lazy var toolBarView: LXStatusToolBarView = LXStatusToolBarView()
    
    //转发微博
    private lazy var retweetView: LXStatusRetweetView = LXStatusRetweetView()

}
